<!-- 项目审批 -->
<template>
	<view style="margin-bottom: 100rpx">
		<cu-custom bgColor="bg-blue2" :isBack="true" @back="backedPage"><block slot="backText">返回</block><block slot="content">移库码单编辑</block></cu-custom>
		<form @submit="onSubmit">
			<view class="cu-form-group">
				<view class="title">原数量</view>
				<input v-model="item.qtyOriginal" placeholder="输入原数量" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">可移数量</view>
				<input v-model="item.qtyAvailable" placeholder="输入可移数量" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">移库数量</view>
				<input v-model="item.qtyActual" placeholder="输入移库数量" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">原重量</view>
				<input v-model="item.weightOriginal" placeholder="输入原重量" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">可移重量</view>
				<input v-model="item.weightAvailable" placeholder="输入可移重量" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">移库重量</view>
				<input v-model="item.weightActual" placeholder="输入移库重量" name="input"></input>
			</view>			
			<view class="cu-form-group">
				<view class="title">原堆号</view>
				<input v-model="item.heapNumOrg" placeholder="输入原堆号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新堆号</view>
				<input v-model="item.heapNumNew" placeholder="输入新堆号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">原层号</view>
				<input v-model="item.layerNumOrg" placeholder="选择原层号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新层号</view>
				<input v-model="item.layerNumNew" placeholder="选择新层号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">原码单号</view>
				<input v-model="item.stackNumOrg" placeholder="输入原码单号" name="input"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">新码单号</view>
				<input v-model="item.stackNumNew" placeholder="输入新码单号" name="input"></input>
			</view>			
			<view>
				<button form-type="submit" class="cu-btn block bg-blue margin-tb-sm lg" :disabled="loading" type="">
					<text :class="loading ? 'cuIcon-loading2 cuIconfont-spin' : ''"></text>保存</button>
			</view>
		</form>
	</view>
</template>

<script>
	import uniTag from '@/components/uni-tag/uni-tag.vue'
	import uniIcons from '@/components/uni-icons/uni-icons.vue'
	import uniCombox from '@/components/uni-combox/uni-combox.vue'

	import {
		mapGetters
	} from 'vuex'
	import {
		getRandomArrayElements,
		filePreview,
		formatStatus
	} from '@/utils/index.js'
	import { formatDate } from '@/utils/datetime.js'
	export default {
		components: {
			uniTag,
			uniIcons,
			uniCombox
		},
		computed: mapGetters(['user']),
		data() {
			return {
				item: { },
				goodsPosiChngBillId:0,
				detailId: 0,
				stackId:0,
				loading: false
			}
		},
		onLoad(options) {
			this.goodsPosiChngBillId = options.goodsPosiChngBillId
			this.detailId = options.detailId
			this.stackId = options.stackId
			if (this.detailId >0 && this.stackId > 0) {
				this.$minApi.getGoodsPosiChngStack(this.stackId).then(res => {
					const data = res
					if (data) {
						this.item = data
					}
				}).catch(err => {
					console.log(err)
				}).finally(() => {
				})
			} else {
				uni.setNavigationBarTitle({
				title: '新增移库码单'
				})
			}
			
		},
		methods: {
			filePreview,
			formatStatus,
			formatDate,
			onSubmit(e) {  
				
				this.loading = true
				const _this = this
				
				this.item.qtyOriginal = Number(this.item.qtyOriginal)
				this.item.qtyAvailable = Number(this.item.qtyAvailable)
				this.item.qtyActual = Number(this.item.qtyActual)
				this.item.weightOriginal = Number(this.item.weightOriginal)
				this.item.weightAvailable = Number(this.item.weightAvailable)
				this.item.weightActual = Number(this.item.weightActual)
				
				this.item.goodsPosiChngDetailID = Number(this.detailId)
				this.item.inStockGoodsStackID = 1 // 从库存码单表带出来
				
				if (this.stackId > 0) {				
					
					_this.$minApi.updateGoodsPosiChngStack(this.item.id, this.item).then(res => {
						uni.showToast({
						    title: '修改成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsPosiChngBill/stack?goodsPosiChngBillId=${this.goodsPosiChngBillId}&detailId=${this.detailId}&stackId=0`
							})
						}, 2000)

					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '修改失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				} else {
					
					_this.$minApi.createGoodsPosiChngStack(this.item).then(res => {
						uni.showToast({
						    title: '添加成功',
						    duration: 2000
						})
						
						setTimeout(() => {
							uni.redirectTo({
								url: `/pages/todo/goodsPosiChngBill/stack?goodsPosiChngBillId=${this.goodsPosiChngBillId}&detailId=${this.detailId}&stackId=0`
							})
						}, 2000)
					}).catch(err => {
						uni.showToast({
							icon: 'none',
						    title: '添加失败',
						    duration: 2000
						})
						console.log(err)
					}).finally(() => {
						this.loading = false
					})
				}
				
			},
			backedPage() {
				uni.redirectTo({
					url: `/pages/goodsPosiChngBill/stack`
				})
			}
		}
	}
</script>

<style>

</style>
